package com.hm.health.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.hm.health.R;
import com.goodix.http.HttpConstants;
import com.goodix.http.RawDataManager;
import com.hm.health.MyApplication;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import rx.Subscription;

/**
 * Created by jiaguang on 2018/11/20.
 */

public class BaseActivity extends RxAppCompatActivity {

    private TextView mTvTitle;
    protected Button mLeftBackBtn;
    protected Context mContext;
    private Subscription mSubscription;
    private boolean mIsUploadSuccess = false;
    PowerManager.WakeLock wakeLock = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivityEx(this);
        initRawdataManager();
        mContext = this;
        acquireWakeLock();
    }

    protected void initActionBar(String title) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        setCenterTitle(title);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initRawdataManager() {
        SharedPreferences preferences =getSharedPreferences(HttpConstants.SHARED_PREFRENCE_NAME_USER, Context.MODE_PRIVATE);
        long token = preferences.getLong(HttpConstants.SHARED_PREFRENCE_KEY_USERTOKEN, 0);
        String userName = preferences.getString(HttpConstants.SHARED_PREFRENCE_KEY_USERNAME, "");
        RawDataManager.setUserAndToken(userName, token);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mSubscription && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
//        KeepAliveManager.getInstance().hiddenOnePixelActivity();
        MyApplication.getInstance().removeActivity(this);
        releaseWakeLock();
    }

    public void replaceFragment(int resId, Fragment fragment, boolean isAddToBackStack) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(resId, fragment);
        if (isAddToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }

    public void addFragment(int resId, Fragment fragment, boolean isAddToBackStack) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(resId, fragment);
        if (isAddToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }

    private void acquireWakeLock() {
        if (null == wakeLock) {
            PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
                    "PostLocationService");
            if (null != wakeLock) {
                wakeLock.acquire();
            }
        }
    }

    private void releaseWakeLock() {
        if (null != wakeLock) {
            wakeLock.release();
            wakeLock = null;
        }
    }

    /**
     * 显示标题
     * @param resId
     */
    public void setCenterTitle(int resId) {
        String title = getResources().getString(resId);
        setCenterTitle(title);
    }

    /**
     * 显示标题
     * @param title
     */
    public void setCenterTitle(String title) {
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.custom_action_bar);
        TextView tvTitle = getSupportActionBar().getCustomView().findViewById(R.id.tv_title);
        if (null != tvTitle) {
            tvTitle.setText(title);
        }
    }
}
